﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace WebPALTT.Lib
{
    /*
     * Class: ActionItem_Script
     * 
     * This class represents a script action and inherits from @link[ActionItem.cs.htm, ActionItem].
     * 
     * ++{{NB:}} Changes to most attribute of a script action during runtime will not alter the runtime behavior.++
     * ++The code from a script action is compiled at the start of a test.++
     * ++Changes to the MethodName property at runtime may cause errors.++
     */
    [Serializable()]
    public class ActionItem_Script : ActionItem
    {
        public override ActionType ActionType { get { return ActionType.Script; } }

        List<string> mCompileParameters;
        List<string> mUsingStatements;
        string mMethod;
        string mCode;

        /// <summary>
        /// Gets the compile parameters for the script.
        /// </summary>
        [Editor(@"System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))]
        public List<string> CompileParameters { get { return mCompileParameters; } }

        /// <summary>
        /// Gets the using statements for the method.
        /// </summary>
        [Editor(@"System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))]
        public List<string> UsingStatements { get { return mUsingStatements; } }

        /// <summary>
        /// Gets or sets the name of the method created / run for this action.
        /// </summary>
        [DescriptionAttribute("The name of the method to call when running script.")]
        public string MethodName { get { return mMethod; } set { mMethod = value; } }

        /// <summary>
        /// Gets or sets the code inside the method.
        /// </summary>
        [BrowsableAttribute(false)]
        public string Code { get { return mCode; } set { mCode = value; } }

        public ActionItem_Script(String name) : this(name, null, null, "", "") { }

        public ActionItem_Script(String name, string[] parameters, string[] usingstatements, string startmethod, string code)
            : base(name)
        {
            mCompileParameters = new List<string> { };
            mUsingStatements = new List<string> { };
            if (parameters != null)
                mCompileParameters.AddRange(parameters);
            if (usingstatements != null)
                mUsingStatements.AddRange(usingstatements);
            mMethod = startmethod;
            mCode = code;
        }

        public ActionItem_Script(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            mCompileParameters = (List<string>)info.GetValue("Parameters", typeof(List<string>));
            mUsingStatements = (List<string>)info.GetValue("UsingStatements", typeof(List<string>));
            mMethod = (string)info.GetValue("Method", typeof(string));
            mCode = (string)info.GetValue("Code", typeof(string));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            base.GetObjectData(info, ctxt);
            info.AddValue("Parameters", mCompileParameters);
            info.AddValue("UsingStatements", mUsingStatements);
            info.AddValue("Method", mMethod);
            info.AddValue("Code", mCode);
        }

        public override string[] Validate()
        {
            List<string> errors = new List<string> { };
            if (Name.Equals("Then") || Name.Equals("Else") || Name.Equals("Actions"))
                errors.Add("Error: action[" + Name + "(" + ActionType + ")] is using a reserverd name.");

            if (MethodName.IndexOfAny(new char[] { ' ', '(', ')', '[', ']', '{', '}', ';', ':', '.', ',', '\\', '/' }) > -1)
                errors.Add("Error: action[" + Name + "(" + ActionType + ")].StartMethod contains invalid characters.");

            return errors.ToArray();
        }

        public override ActionItem GetChild(Guid key) { return null; }
    }
}
